package com.hfy.kandian.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfy.kandian.dao.representation.BookRepresentation;
import com.hfy.kandian.entity.Bookshelf;
import com.hfy.kandian.mapper.BookshelfMapper;
import com.hfy.kandian.service.BookService;
import com.hfy.kandian.service.BookshelfService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAN XIAOYANG
 * @since 2020-12-30
 */
@Service
public class BookshelfServiceImpl extends ServiceImpl<BookshelfMapper, Bookshelf> implements BookshelfService {
    @Autowired
    private BookshelfMapper bookshelfMapper;


    @Override
    public int isCollected(int userId, int bookId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("book_id",bookId);
        return (ObjectUtils.isEmpty(bookshelfMapper.selectOne(queryWrapper)))?0:1;
    }

    @Override
    public int collect(int userId, int bookId) {
        Bookshelf bookshelf = new Bookshelf();
        bookshelf.setUserId(userId).addOneBookId(bookId);
        return bookshelfMapper.insert(bookshelf);

    }

    @Override
    public int delete(int userId, int bookId) {
        Bookshelf bookshelf = bookshelfMapper.selectOne(new LambdaQueryWrapper<Bookshelf>().eq(Bookshelf::getUserId,userId));
        bookshelf.deleteOneBookId(bookId);
        return bookshelfMapper.updateById(bookshelf);
    }

    @Override
    public List<Integer> show(int userId) {
        Bookshelf bookshelf = bookshelfMapper.selectOne(new LambdaQueryWrapper<Bookshelf>().eq(Bookshelf::getUserId,userId));
        return bookshelf.getBookIdList();
    }


}
